<?php
/*
Copyright (C) 2014 by Joey Sabey (GameFreak7744@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
class Hook {
	//Version string...
	const VERSION = 'trunk';

    const TAG_OPEN          = '[';
    const TAG_CLOSE         = ']';
    const TAG_MAGIC_CHAR    = '!';
    const TAG_SEPARATOR     = ':';

    protected $id;
    protected $template = null;
    protected $children = [];

    public function __construct($id) {
        $id = explode(self::TAG_SEPARATOR, $id);
        $this->id = $id[0];
        $this->template = $id[1];
    }

    public function getTemplate() {
        return $this->template;
    }

    public function getID() {
        return $this->id;
    }

    public function getChildren() {
        return $this->children;
    }

    public function getHookString() {
        if($this->isComplex())
            $template = self::TAG_SEPARATOR.$this->template;
        return self::TAG_OPEN.self::TAG_MAGIC_CHAR.$this->getID().$template.self::TAG_CLOSE;
    }

    //Returns an array of all the fully qualified valid insert strings for the hook
    public function getInsertStrings() {
        if($this->isBasic())
            return [$this->getID()];
        foreach($this->getChildren() as $hook) {
            foreach($hook->getInsertStrings() as $s) {
                $inserts[] = $this->getID().self::TAG_SEPARATOR.$s;
            }
        }
        return $inserts;
    }

    //Returns a regex that will match all possible inserts(?)
    public function getInsertRegex($mode = 0) {
        $regex = $this->getID();
        if($this->isComplex()) {
            $regex .= self::TAG_SEPARATOR;
            if($this->countChildren() > 1)
                $regex .= '(?:';
            foreach($this->getChildren() as $child) {
                $children[] = $child->getInsertRegex(1);
            }
            $regex .= implode('|', $children);
            if($this->countChildren() > 1)
                $regex .= ')';
        }
        if($mode == 0)
            return "/^$regex$/U";
        return $regex;
    }

    public function addChild($child) {
        $this->children[] = $child;
    }

    public function countChildren() {
        return count($this->getChildren());
    }

    public function isBasic() {
        return $this->getTemplate() == null;
    }

    public function isComplex() {
        return $this->getTemplate() != null;
    }

    public static function getBasicRegex() {
        return self::getRegexOpen().'(\w+)'.self::getRegexClose();
    }

    public static function getComplexRegex() {
        return self::getRegexOpen().'(\w+'.self::TAG_SEPARATOR.'[\w.]+)'.self::getRegexClose();
    }

    public static function getBasicInsertRegex() {
        return '/^\w+$/U';
    }

    public static function getComplexInsertRegex() {
        return '/^\w+(?:'.self::TAG_SEPARATOR.'[\w]+)+$/U';
    }

    protected static function getRegexOpen() {
        return '/\\'.self::TAG_OPEN.self::TAG_MAGIC_CHAR;
    }

    protected static function getRegexClose() {
        return '\\'.self::TAG_CLOSE.'/U';
    }
}
?>
